package com.arethusa;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

import simpleorm.core.SConnection;
import simpleorm.core.SPreparedStatement;
import simpleorm.core.SResultSet;
import simpleorm.core.SValidationException;

import com.arethusa.langebaandb.Categorie;
import com.arethusa.langebaandb.Programma;
import com.arethusa.langebaandb.ProgrammaBean;
import com.arethusa.langebaandb.Programmanummers;
import com.arethusa.langebaandb.Start;
import com.arethusa.langebaandb.StartBean;
import com.arethusa.langebaandb.StatistiekenperprogrammaBean;
import com.ibm.j2x.swing.util.TableUtilities;
import com.ibm.j2x.util.CollectionUtilities;

public class OnderhoudProgramma extends JPanel {

	private static final long serialVersionUID = 1L;
	private JPanel jPanel = null;
	private JScrollPane jScrollPane = null;
	private JTable jTable = null;
	private JPanel jPanel2 = null;
	private JButton invoerButton = null;
	private JButton wijzigButton = null;
	private JButton verwijderButton = null;
	private JLabel jLabel = null;
	private JTextField progNrTextField = null;
	private JLabel jLabel1 = null;
	private JTextField suffixTextField = null;
	private JLabel jLabel2 = null;
	private JTextField titleTextField = null;
	private JLabel jLabel3 = null;
	private JLabel jLabel4 = null;
	private JTextField afstandTextField = null;
	private JComboBox slagComboBox = null;
	private JLabel jLabel5 = null;
	private JTextField capKleurTextField = null;
	private JLabel jLabel6 = null;
	private JTextField capKleurKNZBTextField = null;
	private JLabel jLabel7 = null;
	private JLabel jLabel8 = null;
	private JComboBox leeftijdVanComboBox = null;
	private JComboBox leeftijdTMComboBox = null;
	private JLabel jLabel9 = null;
	private JComboBox sexComboBox = null;
	private JButton herstelButton = null;
	private JLabel jLabel10 = null;
	private JComboBox typeComboBox = null;
	private List<ProgrammaBean> olist;
	private String sortBy = "ProgNr,Suffix";
	private Boolean sortAscending = new Boolean(true);
	private JLabel jLabel11 = null;
	private JTextField nrPrijzenTextField = null;
	private JLabel jLabel12 = null;
	private JTextField nrStartsTextField = null;
	private JLabel jLabel13 = null;
	private JTextField nrLeencapsTextField = null;
	private JButton berekenButton = null;
	private JButton capNummeringButton = null;
	private int selection;
	private StatistiekenperprogrammaBean stats = new StatistiekenperprogrammaBean();

	private class InvoerButtonActionListener implements ActionListener {

		public void actionPerformed(ActionEvent arg0) {
			ProgrammaBean cur = new ProgrammaBean();
			try {
				cur.setPrognr(new Integer(progNrTextField.getText()));
				cur.setSuffix(suffixTextField.getText());
				cur.setType((String)typeComboBox.getSelectedItem());
				cur.setTitel(titleTextField.getText());
				cur.setAfstand(new Integer(afstandTextField.getText()));
				cur.setCapkleur(capKleurTextField.getText());
				cur.setCapkleurknzb(capKleurKNZBTextField.getText());
				cur.setLeeftijdtm((String)leeftijdTMComboBox.getSelectedItem());
				cur.setLeeftijdvan((String)leeftijdVanComboBox.getSelectedItem());
				cur.setSex((String)sexComboBox.getSelectedItem());
				cur.setSlag((String)slagComboBox.getSelectedItem());
				cur.create();
				fillTable(sortBy, sortAscending);
				clearInvoer();
			} catch (SValidationException e1) {
				JOptionPane.showMessageDialog(null,
					e1.getMessage(),
					"Validatie", JOptionPane.ERROR_MESSAGE);
			} catch (NumberFormatException e2) {
				JOptionPane.showMessageDialog(null,
					"Programma nummer moet een nummer zijn.",
					"Validatie", JOptionPane.ERROR_MESSAGE);
			}
		}
	}
	
	private class WijzigButtonActionListener implements ActionListener {

		public void actionPerformed(ActionEvent arg0) {
			ProgrammaBean cur = new ProgrammaBean();
			cur.setPrognr(new Integer(progNrTextField.getText()));
			cur.setSuffix(suffixTextField.getText());
			try {
				cur.setTitel(titleTextField.getText());
				cur.setType((String)typeComboBox.getSelectedItem());
				cur.setAfstand(new Integer(afstandTextField.getText()));
				cur.setCapkleur(capKleurTextField.getText());
				cur.setCapkleurknzb(capKleurKNZBTextField.getText());
				cur.setLeeftijdtm((String)leeftijdTMComboBox.getSelectedItem());
				cur.setLeeftijdvan((String)leeftijdVanComboBox.getSelectedItem());
				cur.setSex((String)sexComboBox.getSelectedItem());
				cur.setSlag((String)slagComboBox.getSelectedItem());
				cur.update();
				// Aantal prijzen gewijzigd?
				Long nrprz = new Long(getNrPrijzenTextField().getText());
				Long orgnrprz = stats.getNrprijzen();
				if( nrprz.compareTo(orgnrprz) != 0) {
//					System.out.println("Aantal prijzen is gewijzigd van "+orgnrprz+" naar "+nrprz);
					stats.setPrognr(new Integer(progNrTextField.getText()));
					stats.setSuffix(suffixTextField.getText());
					stats.read();
					stats.setNrprijzen(nrprz);
					stats.update();
				}
				fillTable(sortBy, sortAscending);
				clearInvoer();
				getJTable().setRowSelectionInterval(selection, selection);
			} catch (SValidationException e1) {
				JOptionPane.showMessageDialog(null,
					e1.getMessage(),
					"Validatie", JOptionPane.ERROR_MESSAGE);
			} catch (NumberFormatException e2) {
				JOptionPane.showMessageDialog(null,
					"Afstand moet een getal zijn.",
					"Validatie", JOptionPane.ERROR_MESSAGE);
			}
		}
	}
	
	private class VerwijderButtonActionListener implements ActionListener {

		public void actionPerformed(ActionEvent arg0) {
			if(JOptionPane.showConfirmDialog(null,
					"Zeker weten?","Verwijderen",
					JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_CANCEL_OPTION ) return;
			ProgrammaBean cur = new ProgrammaBean();
			cur.setPrognr(new Integer(progNrTextField.getText()));
			cur.setSuffix(suffixTextField.getText());
			try {
				cur.setType((String)typeComboBox.getSelectedItem());
				cur.setTitel(titleTextField.getText());
	    		cur.setCapkleur(capKleurTextField.getText());
	    		cur.setCapkleurknzb(capKleurKNZBTextField.getText());
	    		cur.setAfstand(new Integer(afstandTextField.getText()));
	    		cur.setSex((String)sexComboBox.getSelectedItem());
	    		cur.setSlag((String)slagComboBox.getSelectedItem());
	    		cur.setLeeftijdvan((String)leeftijdVanComboBox.getSelectedItem());
	    		cur.setLeeftijdtm((String)leeftijdTMComboBox.getSelectedItem());
				cur.delete();
				fillTable(sortBy, sortAscending);
				clearInvoer();
			} catch (SValidationException e) {
				JOptionPane.showMessageDialog(null,
						e.getMessage(),
						"Validatie", JOptionPane.ERROR_MESSAGE);
			}
		}
	}
	
	private class CapsNummerenButtonActionListener implements ActionListener {

		public void actionPerformed(ActionEvent arg0) {
			boolean hasBegun = SConnection.getConnection().hasBegun();
			if( ! hasBegun ) { SConnection.begin(); }
			SPreparedStatement stmtp = Programmanummers.meta.select(
					"1=1", "PROGNR");
			SResultSet resp = stmtp.execute();
			while( resp.hasNext() ) {
				Programmanummers prg = (Programmanummers)resp.getRecord();
				// Haal capkleur op van leencaps
				SPreparedStatement stmtprg = Programma.meta.select(
						"PROGNR="+ prg.get_fldPrognr().toString(),
						"PROGNR");
				SResultSet resprg = stmtprg.execute();
				String capKleur="";
				if(resprg.hasNext()) {
					Programma pr = (Programma)resprg.getRecord();
					capKleur = pr.get_fldCapkleur();
				}
				int startCapNr = 0;
				int eindCapNr = 0;
				Start nrs = null;
				// Haal de capnrs op van de vaste caps
				SPreparedStatement stmtn = Start.meta.select(   
						"PROGNR="+ prg.get_fldPrognr().toString()+" AND Vast=1","CAPNR");
				SResultSet resn = stmtn.execute();
				if(resn.hasNext()) {
					nrs = (Start)resn.getRecord();
					eindCapNr = nrs.get_fldCapnr().intValue();
					if( eindCapNr == 0 ) {
						while ( resn.hasNext() ) {
							nrs = (Start)resn.getRecord();
							eindCapNr = nrs.get_fldCapnr().intValue();
							if( eindCapNr > 0 ) break;
						}
					}
				}
				// Haal de leencap records op
				SPreparedStatement stmt = Start.meta.select(   
						"PROGNR="+ prg.get_fldPrognr().toString()+" AND Vast=0","PROGNR");
				SResultSet res = stmt.execute();
				while( res.hasNext() ) {
					startCapNr++;
					if( startCapNr == eindCapNr ) {
						while( resn.hasNext() ) {
							startCapNr++;
							nrs = (Start)resn.getRecord();
							eindCapNr = nrs.get_fldCapnr().intValue();
							if( eindCapNr == 0 ) {
								while ( resn.hasNext() ) {
									nrs = (Start)resn.getRecord();
									eindCapNr = nrs.get_fldCapnr().intValue();
									if( eindCapNr > 0 ) break;
								}
							}
							if(startCapNr < eindCapNr) break;
						}
						if( startCapNr == eindCapNr || eindCapNr == 0 ) { //er zijn geen vaste caps meer !
							startCapNr++;
							eindCapNr = 1000;
						}
					}
					Start strt = (Start)res.getRecord();
					StartBean sb = new StartBean(strt);
					sb.setCapnr(new Integer(startCapNr));
					sb.setCapkleur(capKleur);
					sb.update();
				}
			}
			if( ! hasBegun ) { SConnection.commit(); }
			JOptionPane.showMessageDialog(null,
					"Alle leencaps zijn genummerd.",
					"Leencaps Nummeren", JOptionPane.INFORMATION_MESSAGE);
		}
	}

	private class BerekenButtonActionListener implements ActionListener {

		public void actionPerformed(ActionEvent arg0) {
			Helper.BepaalNrPrijzen();
			Helper.BepaalNrStarts();
			Helper.BepaalNrLeencaps();
            fillTable(sortBy, sortAscending);
            clearInvoer();
			getJTable().setRowSelectionInterval(selection, selection);
		}
	}

	private class TableSelectionListener implements ListSelectionListener {
	    public void valueChanged(ListSelectionEvent e) {
	        //Ignore extra messages.
	        if (e.getValueIsAdjusting()) return;

	        ListSelectionModel lsm = (ListSelectionModel)e.getSource();
	        if (lsm.isSelectionEmpty()) {
	            //no rows are selected
	        	getInvoerButton().setEnabled(true);
	        	getWijzigButton().setEnabled(false);
	        	getVerwijderButton().setEnabled(false);
	        } else {
	            selection = lsm.getMinSelectionIndex();
	            //selectedRow is selected
	        	getInvoerButton().setEnabled(false);
	        	getWijzigButton().setEnabled(true);
	        	getVerwijderButton().setEnabled(true);
	        	ProgrammaBean r = (ProgrammaBean)olist.get(selection);
	        	getProgNrTextField().setText(r.getPrognr().toString());
	        	getProgNrTextField().setEnabled(false);
	        	getSuffixTextField().setText(r.getSuffix());
	        	getSuffixTextField().setEnabled(false);
	    		getCapKleurTextField().setText(r.getCapkleur());
	    		getCapKleurKNZBTextField().setText(r.getCapkleurknzb());
	    		getTitleTextField().setText(r.getTitel());
	    		getTypeComboBox().setSelectedItem(r.getType());
	    		getSexComboBox().setSelectedItem(r.getSex());
	    		getSlagComboBox().setSelectedItem(r.getSlag());
	    		getLeeftijdVanComboBox().setSelectedItem(r.getLeeftijdvan());
	    		getLeeftijdTMComboBox().setSelectedItem(r.getLeeftijdtm());
	    		getAfstandTextField().setText(r.getAfstand().toString());
				stats.setPrognr(new Integer(progNrTextField.getText()));
				stats.setSuffix(suffixTextField.getText());
				stats.read();
	    		getNrStartsTextField().setText(stats.getNrstarts().toString());
	    		getNrLeencapsTextField().setText(stats.getNrvastecap().toString());
	    		getNrPrijzenTextField().setText(stats.getNrprijzen().toString());
	        }
	    }
	}
	
	private class TableSorter extends MouseAdapter {
        public void mouseClicked(MouseEvent e) {
            TableColumnModel columnModel = jTable.getColumnModel();
            int viewColumn = columnModel.getColumnIndexAtX(e.getX()); 
            int column = jTable.convertColumnIndexToModel(viewColumn); 
            if(e.getClickCount() == 1 && column != -1) {
                sortAscending = new Boolean(!sortAscending.booleanValue());
                sortBy = jTable.getModel().getColumnName(column);
                fillTable(sortBy, sortAscending);
                clearInvoer();
            }
         }
	}

	/**
	 * This is the default constructor
	 */
	public OnderhoudProgramma() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setLayout(new BorderLayout());
		this.setSize(504, 561);
		this.add(getJPanel(), java.awt.BorderLayout.NORTH);
		this.add(getJScrollPane(), java.awt.BorderLayout.CENTER);
		fillTable("ProgNr,Suffix", new Boolean(true));
		fillLeeftijd(getLeeftijdVanComboBox());
		fillLeeftijd(getLeeftijdTMComboBox());
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jPanel = new JPanel();
			jPanel.setLayout(new BorderLayout());
			jPanel.add(getJPanel2(), java.awt.BorderLayout.CENTER);
		}
		return jPanel;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setViewportView(getJTable());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jTable	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private JTable getJTable() {
		if (jTable == null) {
			jTable = new JTable();
			jTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
			jTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
			jTable.setShowGrid(true);
// Ask to be notified of selection changes.
			jTable.getSelectionModel().addListSelectionListener(new TableSelectionListener());
// Make column header click result in sorting ascending (shift-click is descending)
			jTable.setColumnSelectionAllowed(false);
	        TableSorter listSorter = new TableSorter();
			JTableHeader th = jTable.getTableHeader();
	        th.addMouseListener(listSorter); 
		}
		return jTable;
	}

	/**
	 * This method initializes jPanel2	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel2() {
		if (jPanel2 == null) {
			GridBagConstraints gridBagConstraints110 = new GridBagConstraints();
			gridBagConstraints110.gridx = 6;
			gridBagConstraints110.insets = new java.awt.Insets(3,3,3,3);
			gridBagConstraints110.gridy = 5;
			GridBagConstraints gridBagConstraints201 = new GridBagConstraints();
			gridBagConstraints201.gridx = 6;
			gridBagConstraints201.insets = new java.awt.Insets(20,3,20,3);
			gridBagConstraints201.fill = java.awt.GridBagConstraints.BOTH;
			gridBagConstraints201.gridy = 6;
			GridBagConstraints gridBagConstraints191 = new GridBagConstraints();
			gridBagConstraints191.fill = java.awt.GridBagConstraints.NONE;
			gridBagConstraints191.gridy = 6;
			gridBagConstraints191.weightx = 1.0;
			gridBagConstraints191.anchor = java.awt.GridBagConstraints.WEST;
			gridBagConstraints191.insets = new java.awt.Insets(3,3,3,3);
			gridBagConstraints191.ipadx = 20;
			gridBagConstraints191.gridx = 5;
			GridBagConstraints gridBagConstraints181 = new GridBagConstraints();
			gridBagConstraints181.gridx = 4;
			gridBagConstraints181.gridy = 6;
			jLabel13 = new JLabel();
			jLabel13.setText("Leencaps");
			GridBagConstraints gridBagConstraints171 = new GridBagConstraints();
			gridBagConstraints171.fill = java.awt.GridBagConstraints.NONE;
			gridBagConstraints171.gridy = 6;
			gridBagConstraints171.weightx = 1.0;
			gridBagConstraints171.ipadx = 20;
			gridBagConstraints171.insets = new java.awt.Insets(3,3,3,3);
			gridBagConstraints171.anchor = java.awt.GridBagConstraints.WEST;
			gridBagConstraints171.gridx = 3;
			GridBagConstraints gridBagConstraints161 = new GridBagConstraints();
			gridBagConstraints161.gridx = 2;
			gridBagConstraints161.gridy = 6;
			jLabel12 = new JLabel();
			jLabel12.setText("Starts");
			GridBagConstraints gridBagConstraints151 = new GridBagConstraints();
			gridBagConstraints151.fill = java.awt.GridBagConstraints.NONE;
			gridBagConstraints151.gridy = 6;
			gridBagConstraints151.weightx = 1.0;
			gridBagConstraints151.insets = new java.awt.Insets(3,3,3,3);
			gridBagConstraints151.anchor = java.awt.GridBagConstraints.WEST;
			gridBagConstraints151.ipadx = 20;
			gridBagConstraints151.gridx = 1;
			GridBagConstraints gridBagConstraints141 = new GridBagConstraints();
			gridBagConstraints141.gridx = 0;
			gridBagConstraints141.anchor = java.awt.GridBagConstraints.EAST;
			gridBagConstraints141.insets = new java.awt.Insets(3,3,3,3);
			gridBagConstraints141.gridy = 6;
			jLabel11 = new JLabel();
			jLabel11.setText("Aantal prijzen");
			GridBagConstraints gridBagConstraints25 = new GridBagConstraints();
			gridBagConstraints25.fill = java.awt.GridBagConstraints.NONE;
			gridBagConstraints25.gridy = 0;
			gridBagConstraints25.weightx = 1.0;
			gridBagConstraints25.ipadx = 10;
			gridBagConstraints25.anchor = java.awt.GridBagConstraints.WEST;
			gridBagConstraints25.gridx = 5;
			GridBagConstraints gridBagConstraints24 = new GridBagConstraints();
			gridBagConstraints24.gridx = 4;
			gridBagConstraints24.insets = new java.awt.Insets(3,3,3,3);
			gridBagConstraints24.anchor = java.awt.GridBagConstraints.EAST;
			gridBagConstraints24.gridy = 0;
			jLabel10 = new JLabel();
			jLabel10.setText("Type");
			GridBagConstraints gridBagConstraints23 = new GridBagConstraints();
			gridBagConstraints23.gridx = 6;
			gridBagConstraints23.insets = new java.awt.Insets(3,3,3,3);
			gridBagConstraints23.fill = java.awt.GridBagConstraints.HORIZONTAL;
			gridBagConstraints23.gridy = 4;
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.insets = new Insets(3, 3, 3, 3);
			gridBagConstraints2.gridy = 3;
			gridBagConstraints2.fill = java.awt.GridBagConstraints.BOTH;
			gridBagConstraints2.gridx = 6;
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints1.gridx = 6;
			gridBagConstraints1.gridy = 2;
			gridBagConstraints1.insets = new Insets(3, 3, 3, 3);
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints.gridx = 6;
			gridBagConstraints.gridy = 0;
			gridBagConstraints.insets = new Insets(3, 3, 3, 3);
			GridBagConstraints gridBagConstraints22 = new GridBagConstraints();
			gridBagConstraints22.gridy = 3;
			gridBagConstraints22.weightx = 1.0;
			gridBagConstraints22.insets = new java.awt.Insets(3,3,3,3);
			gridBagConstraints22.anchor = java.awt.GridBagConstraints.WEST;
			gridBagConstraints22.ipadx = 15;
			gridBagConstraints22.gridx = 5;
			GridBagConstraints gridBagConstraints21 = new GridBagConstraints();
			gridBagConstraints21.gridx = 4;
			gridBagConstraints21.insets = new java.awt.Insets(3,3,3,3);
			gridBagConstraints21.anchor = java.awt.GridBagConstraints.EAST;
			gridBagConstraints21.gridy = 3;
			jLabel9 = new JLabel();
			jLabel9.setText("Sex");
			GridBagConstraints gridBagConstraints20 = new GridBagConstraints();
			gridBagConstraints20.fill = java.awt.GridBagConstraints.HORIZONTAL;
			gridBagConstraints20.gridy = 5;
			gridBagConstraints20.weightx = 1.0;
			gridBagConstraints20.gridwidth = 2;
			gridBagConstraints20.insets = new java.awt.Insets(3,3,3,3);
			gridBagConstraints20.gridx = 4;
			GridBagConstraints gridBagConstraints19 = new GridBagConstraints();
			gridBagConstraints19.fill = java.awt.GridBagConstraints.HORIZONTAL;
			gridBagConstraints19.gridy = 5;
			gridBagConstraints19.weightx = 1.0;
			gridBagConstraints19.gridwidth = 2;
			gridBagConstraints19.insets = new java.awt.Insets(3,3,3,3);
			gridBagConstraints19.gridx = 1;
			GridBagConstraints gridBagConstraints18 = new GridBagConstraints();
			gridBagConstraints18.gridx = 3;
			gridBagConstraints18.anchor = java.awt.GridBagConstraints.EAST;
			gridBagConstraints18.insets = new java.awt.Insets(3,3,3,3);
			gridBagConstraints18.gridy = 5;
			jLabel8 = new JLabel();
			jLabel8.setText("tot en met");
			GridBagConstraints gridBagConstraints17 = new GridBagConstraints();
			gridBagConstraints17.gridx = 0;
			gridBagConstraints17.anchor = java.awt.GridBagConstraints.EAST;
			gridBagConstraints17.insets = new java.awt.Insets(3,3,3,3);
			gridBagConstraints17.gridy = 5;
			jLabel7 = new JLabel();
			jLabel7.setText("Leeftijd van");
			GridBagConstraints gridBagConstraints16 = new GridBagConstraints();
			gridBagConstraints16.fill = java.awt.GridBagConstraints.BOTH;
			gridBagConstraints16.gridy = 4;
			gridBagConstraints16.weightx = 1.0;
			gridBagConstraints16.insets = new java.awt.Insets(3,3,3,3);
			gridBagConstraints16.gridwidth = 2;
			gridBagConstraints16.gridx = 4;
			GridBagConstraints gridBagConstraints15 = new GridBagConstraints();
			gridBagConstraints15.gridx = 3;
			gridBagConstraints15.anchor = java.awt.GridBagConstraints.EAST;
			gridBagConstraints15.insets = new java.awt.Insets(3,3,3,3);
			gridBagConstraints15.gridy = 4;
			jLabel6 = new JLabel();
			jLabel6.setText("CapKleur KNZB");
			GridBagConstraints gridBagConstraints14 = new GridBagConstraints();
			gridBagConstraints14.fill = java.awt.GridBagConstraints.BOTH;
			gridBagConstraints14.gridy = 4;
			gridBagConstraints14.weightx = 1.0;
			gridBagConstraints14.insets = new java.awt.Insets(3,3,3,3);
			gridBagConstraints14.gridwidth = 2;
			gridBagConstraints14.gridx = 1;
			GridBagConstraints gridBagConstraints13 = new GridBagConstraints();
			gridBagConstraints13.gridx = 0;
			gridBagConstraints13.anchor = java.awt.GridBagConstraints.EAST;
			gridBagConstraints13.insets = new java.awt.Insets(3,3,3,3);
			gridBagConstraints13.gridy = 4;
			jLabel5 = new JLabel();
			jLabel5.setText("CapKleur");
			GridBagConstraints gridBagConstraints12 = new GridBagConstraints();
			gridBagConstraints12.gridy = 3;
			gridBagConstraints12.weightx = 1.0;
			gridBagConstraints12.insets = new java.awt.Insets(3,3,3,3);
			gridBagConstraints12.anchor = java.awt.GridBagConstraints.WEST;
			gridBagConstraints12.ipadx = 20;
			gridBagConstraints12.gridx = 3;
			GridBagConstraints gridBagConstraints10 = new GridBagConstraints();
			gridBagConstraints10.gridy = 3;
			gridBagConstraints10.weightx = 1.0;
			gridBagConstraints10.insets = new java.awt.Insets(3,3,3,3);
			gridBagConstraints10.anchor = java.awt.GridBagConstraints.WEST;
			gridBagConstraints10.ipadx = 40;
			gridBagConstraints10.fill = java.awt.GridBagConstraints.VERTICAL;
			gridBagConstraints10.gridx = 1;
			GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
			gridBagConstraints11.gridx = 2;
			gridBagConstraints11.anchor = java.awt.GridBagConstraints.EAST;
			gridBagConstraints11.insets = new java.awt.Insets(3,3,3,3);
			gridBagConstraints11.gridy = 3;
			jLabel4 = new JLabel();
			jLabel4.setText("Slag");
			GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
			gridBagConstraints9.gridx = 0;
			gridBagConstraints9.insets = new java.awt.Insets(3,3,3,3);
			gridBagConstraints9.anchor = java.awt.GridBagConstraints.EAST;
			gridBagConstraints9.gridy = 3;
			jLabel3 = new JLabel();
			jLabel3.setText("Afstand");
			GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
			gridBagConstraints8.gridy = 2;
			gridBagConstraints8.weightx = 1.0;
			gridBagConstraints8.insets = new java.awt.Insets(3,3,3,3);
			gridBagConstraints8.anchor = java.awt.GridBagConstraints.WEST;
			gridBagConstraints8.fill = java.awt.GridBagConstraints.BOTH;
			gridBagConstraints8.gridwidth = 5;
			gridBagConstraints8.gridx = 1;
			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
			gridBagConstraints7.gridx = 0;
			gridBagConstraints7.anchor = java.awt.GridBagConstraints.EAST;
			gridBagConstraints7.insets = new java.awt.Insets(3,3,3,3);
			gridBagConstraints7.gridy = 2;
			jLabel2 = new JLabel();
			jLabel2.setText("Titel");
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints6.fill = java.awt.GridBagConstraints.VERTICAL;
			gridBagConstraints6.gridy = 0;
			gridBagConstraints6.weightx = 1.0;
			gridBagConstraints6.insets = new java.awt.Insets(3,3,3,3);
			gridBagConstraints6.anchor = java.awt.GridBagConstraints.WEST;
			gridBagConstraints6.ipadx = 20;
			gridBagConstraints6.gridx = 3;
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.gridwidth = 1;
			gridBagConstraints5.gridy = 0;
			gridBagConstraints5.anchor = java.awt.GridBagConstraints.EAST;
			gridBagConstraints5.insets = new java.awt.Insets(3,3,3,3);
			gridBagConstraints5.gridx = 2;
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.gridy = 0;
			gridBagConstraints4.weightx = 1.0;
			gridBagConstraints4.anchor = java.awt.GridBagConstraints.WEST;
			gridBagConstraints4.insets = new java.awt.Insets(3,3,3,3);
			gridBagConstraints4.ipadx = 20;
			gridBagConstraints4.fill = java.awt.GridBagConstraints.VERTICAL;
			gridBagConstraints4.gridx = 1;
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.gridx = 0;
			gridBagConstraints3.insets = new java.awt.Insets(3,3,3,3);
			gridBagConstraints3.anchor = java.awt.GridBagConstraints.EAST;
			gridBagConstraints3.gridy = 0;
			jLabel1 = new JLabel();
			jLabel1.setText("Suffix");
			jLabel = new JLabel();
			jLabel.setText("Nummer");
			jPanel2 = new JPanel();
			jPanel2.setLayout(new GridBagLayout());
			jPanel2.add(jLabel, gridBagConstraints3);
			jPanel2.add(getProgNrTextField(), gridBagConstraints4);
			jPanel2.add(jLabel1, gridBagConstraints5);
			jPanel2.add(getSuffixTextField(), gridBagConstraints6);
			jPanel2.add(jLabel10, gridBagConstraints24);
			jPanel2.add(getTypeComboBox(), gridBagConstraints25);
			jPanel2.add(jLabel2, gridBagConstraints7);
			jPanel2.add(getTitleTextField(), gridBagConstraints8);
			jPanel2.add(jLabel3, gridBagConstraints9);
			jPanel2.add(getAfstandTextField(), gridBagConstraints10);
			jPanel2.add(jLabel4, gridBagConstraints11);
			jPanel2.add(getSlagComboBox(), gridBagConstraints12);
			jPanel2.add(jLabel9, gridBagConstraints21);
			jPanel2.add(getSexComboBox(), gridBagConstraints22);
			jPanel2.add(jLabel5, gridBagConstraints13);
			jPanel2.add(getCapKleurTextField(), gridBagConstraints14);
			jPanel2.add(jLabel6, gridBagConstraints15);
			jPanel2.add(getCapKleurKNZBTextField(), gridBagConstraints16);
			jPanel2.add(jLabel7, gridBagConstraints17);
			jPanel2.add(getLeeftijdVanComboBox(), gridBagConstraints19);
			jPanel2.add(jLabel8, gridBagConstraints18);
			jPanel2.add(getLeeftijdTMComboBox(), gridBagConstraints20);
			jPanel2.add(getInvoerButton(), gridBagConstraints);
			jPanel2.add(getWijzigButton(), gridBagConstraints1);
			jPanel2.add(getVerwijderButton(), gridBagConstraints2);
			jPanel2.add(getHerstelButton(), gridBagConstraints23);
			jPanel2.add(jLabel11, gridBagConstraints141);
			jPanel2.add(getNrPrijzenTextField(), gridBagConstraints151);
			jPanel2.add(jLabel12, gridBagConstraints161);
			jPanel2.add(getNrStartsTextField(), gridBagConstraints171);
			jPanel2.add(jLabel13, gridBagConstraints181);
			jPanel2.add(getNrLeencapsTextField(), gridBagConstraints191);
			jPanel2.add(getBerekenButton(), gridBagConstraints201);
			jPanel2.add(getCapNummeringButton(), gridBagConstraints110);
		}
		return jPanel2;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getInvoerButton() {
		if (invoerButton == null) {
			invoerButton = new JButton();
			invoerButton.setText("Voer in");
			invoerButton.addActionListener(new InvoerButtonActionListener());
		}
		return invoerButton;
	}

	/**
	 * This method initializes jButton1	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getWijzigButton() {
		if (wijzigButton == null) {
			wijzigButton = new JButton();
			wijzigButton.setText("Wijzig");
			wijzigButton.setEnabled(false);
			wijzigButton.addActionListener(new WijzigButtonActionListener());
		}
		return wijzigButton;
	}

	/**
	 * This method initializes jButton2	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getVerwijderButton() {
		if (verwijderButton == null) {
			verwijderButton = new JButton();
			verwijderButton.setText("Verwijder");
			verwijderButton.setEnabled(false);
			verwijderButton.addActionListener(new VerwijderButtonActionListener());
		}
		return verwijderButton;
	}

	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getProgNrTextField() {
		if (progNrTextField == null) {
			progNrTextField = new JTextField();
		}
		return progNrTextField;
	}

	/**
	 * This method initializes jTextField1	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getSuffixTextField() {
		if (suffixTextField == null) {
			suffixTextField = new JTextField();
		}
		return suffixTextField;
	}

	/**
	 * This method initializes jTextField2	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getTitleTextField() {
		if (titleTextField == null) {
			titleTextField = new JTextField();
		}
		return titleTextField;
	}

	/**
	 * This method initializes jTextField3	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getAfstandTextField() {
		if (afstandTextField == null) {
			afstandTextField = new JTextField();
		}
		return afstandTextField;
	}

	/**
	 * This method initializes jComboBox	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getSlagComboBox() {
		if (slagComboBox == null) {
			slagComboBox = new JComboBox();
			slagComboBox.addItem("vrij");
			slagComboBox.addItem("school");
			slagComboBox.addItem("rug");
			slagComboBox.addItem("vlinder");
			slagComboBox.addItem("wissel");
		}
		return slagComboBox;
	}

	/**
	 * This method initializes jTextField4	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getCapKleurTextField() {
		if (capKleurTextField == null) {
			capKleurTextField = new JTextField();
		}
		return capKleurTextField;
	}

	/**
	 * This method initializes jTextField5	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getCapKleurKNZBTextField() {
		if (capKleurKNZBTextField == null) {
			capKleurKNZBTextField = new JTextField();
		}
		return capKleurKNZBTextField;
	}

	/**
	 * This method initializes jComboBox1	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getLeeftijdVanComboBox() {
		if (leeftijdVanComboBox == null) {
			leeftijdVanComboBox = new JComboBox();
		}
		return leeftijdVanComboBox;
	}

	/**
	 * This method initializes jComboBox2	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getLeeftijdTMComboBox() {
		if (leeftijdTMComboBox == null) {
			leeftijdTMComboBox = new JComboBox();
		}
		return leeftijdTMComboBox;
	}

	/**
	 * This method initializes jComboBox3	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getSexComboBox() {
		if (sexComboBox == null) {
			sexComboBox = new JComboBox();
			sexComboBox.addItem("H");
			sexComboBox.addItem("D");
			sexComboBox.addItem("M");
		}
		return sexComboBox;
	}

	/**
	 * This method initializes jButton3	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getHerstelButton() {
		if (herstelButton == null) {
			herstelButton = new JButton();
			herstelButton.setText("Herstel");
			herstelButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					fillTable(sortBy, sortAscending);
					clearInvoer();
				}
			});
		}
		return herstelButton;
	}

	/**
	 * This method initializes typeComboBox	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getTypeComboBox() {
		if (typeComboBox == null) {
			typeComboBox = new JComboBox();
			typeComboBox.addItem("W");
			typeComboBox.addItem("P");
		}
		return typeComboBox;
	}

	
	@SuppressWarnings("unchecked")
	private void fillTable(String sortBy, Boolean ascending) {
		List<ProgrammaBean> tmp = new ArrayList<ProgrammaBean>();
		/// Prepare and execute the query
		boolean hasBegun = SConnection.getConnection().hasBegun();
		if( ! hasBegun ) { SConnection.begin(); }
		SPreparedStatement stmt = Programma.meta.select(
				"1=1", sortBy+(ascending.booleanValue()?"":" DESC"));
		SResultSet res = stmt.execute();
		
		/// loop through the results
		while (res.hasNext()) {
			ProgrammaBean strt = new ProgrammaBean(((Programma)res.getRecord()));
			tmp.add(strt);
		}
		if( ! hasBegun ) { SConnection.commit(); }
    	olist = CollectionUtilities.observableList(tmp);
		TableUtilities.setViewToModel("LangebaanModel.xml", "Programma", getJTable(), olist);
		TableColumn col1 = getJTable().getColumnModel().getColumn(0);
		col1.setPreferredWidth(20);		// ProgNr
		TableColumn col2 = getJTable().getColumnModel().getColumn(1);
		col2.setPreferredWidth(20);		// Suffix
		TableColumn col3 = getJTable().getColumnModel().getColumn(2);
		col3.setPreferredWidth(260);	// Titel
		TableColumn col4 = getJTable().getColumnModel().getColumn(3);
		col4.setPreferredWidth(50);		// Capkleur
		TableColumn col5 = getJTable().getColumnModel().getColumn(4);
		col5.setPreferredWidth(50);		// Capkleur Vast
		TableColumn col6 = getJTable().getColumnModel().getColumn(5);
		col6.setPreferredWidth(40);		// Afstand
		TableColumn col7 = getJTable().getColumnModel().getColumn(6);
		col7.setPreferredWidth(50);		// Slag
		TableColumn col8 = getJTable().getColumnModel().getColumn(7);
		col8.setPreferredWidth(30);		// Sex
		TableColumn col9 = getJTable().getColumnModel().getColumn(8);
		col9.setPreferredWidth(80);		// Leeftijd Van
		TableColumn col10 = getJTable().getColumnModel().getColumn(9);
		col10.setPreferredWidth(80);	// Leeftijd TM
	}
	
	private void clearInvoer() {
		getProgNrTextField().setText(null);
		getProgNrTextField().setEnabled(true);
		getSuffixTextField().setText(null);
		getSuffixTextField().setEnabled(true);
		getTitleTextField().setText(null);
		getTypeComboBox().setSelectedIndex(0);
		getSexComboBox().setSelectedIndex(0);
		getSlagComboBox().setSelectedIndex(0);
		getLeeftijdVanComboBox().setSelectedIndex(0);
		getLeeftijdTMComboBox().setSelectedIndex(0);
		getAfstandTextField().setText(null);
		getCapKleurTextField().setText(null);
		getCapKleurKNZBTextField().setText(null);
		getNrStartsTextField().setText(null);
		getNrPrijzenTextField().setText(null);
		getNrLeencapsTextField().setText(null);
	}
	
	private void fillLeeftijd(JComboBox cb) {
		boolean hasBegun = SConnection.getConnection().hasBegun();
		if( ! hasBegun ) { SConnection.begin(); }
		SPreparedStatement stmt = Categorie.meta.select(
				"Sex='D' OR Sex='A'", "Cat");
		SResultSet res = stmt.execute();
		while (res.hasNext()) {
			Categorie ver = (Categorie)res.getRecord();
			cb .addItem( ver.get_fldCat());
		}
		SConnection.commit();
	}

	/**
	 * This method initializes nrPrijzenTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getNrPrijzenTextField() {
		if (nrPrijzenTextField == null) {
			nrPrijzenTextField = new JTextField();
		}
		return nrPrijzenTextField;
	}

	/**
	 * This method initializes nrStartsTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getNrStartsTextField() {
		if (nrStartsTextField == null) {
			nrStartsTextField = new JTextField();
			nrStartsTextField.setEditable(false);
		}
		return nrStartsTextField;
	}

	/**
	 * This method initializes nrLeencapsTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getNrLeencapsTextField() {
		if (nrLeencapsTextField == null) {
			nrLeencapsTextField = new JTextField();
			nrLeencapsTextField.setEditable(false);
		}
		return nrLeencapsTextField;
	}

	/**
	 * This method initializes berekenButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBerekenButton() {
		if (berekenButton == null) {
			berekenButton = new JButton();
			berekenButton.setText("Bereken");
			berekenButton.addActionListener(new BerekenButtonActionListener());
		}
		return berekenButton;
	}

	/**
	 * This method initializes capNummeringButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getCapNummeringButton() {
		if (capNummeringButton == null) {
			capNummeringButton = new JButton();
			
			capNummeringButton.setText("Caps nummeren");
			capNummeringButton.addActionListener(new CapsNummerenButtonActionListener());
		}
		return capNummeringButton;
	}
	
}  //  @jve:decl-index=0:visual-constraint="10,10"
